//我的自动化每日构建脚本

//脚本路径
var path="C:\\DailyBuilding\\";
//使用GetSrc.bat从VSS中获得最新代码,并保存在src中
var getCommand="GetSrc.bat";
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.Run(getCommand,0,true);

//使用BuildSrc.bat中CL或MSDev98构建C或dsp文件
var getCommand="BuildSrc.bat";
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.Run(getCommand,0,true);

var bof="log\\testlog.txt";
var fso = new ActiveXObject("Scripting.FileSystemObject");

var result=CheckMSBuildResult(bof);
Deploy("iotenglib 0.0.1");
QQSendMail("iotenglib 0.0.1", bof, result);
TomSendMail("iotenglib 0.0.1", bof, result);

//通过RunApp.bat执行应用程序
var getCommand="RunApp.bat";
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.Run(getCommand,0,true);

function CheckMSBuildResult(buildOutputFile){
	var ret=false;
	var outputfile;
	outputfile=fso.OpenTextFile(buildOutputFile,1,false)
	if(outputfile!=null){
		var readline;
		var rebuildline="";
		readline=outputfile.ReadLine();
		while(!outputfile.AtEndOfFile){
			if(readline.indexOf(".exe - ") >= 0){
				rebuildline=readline;
				var ss,s,num,failednum;
				ss=rebuildline;
				s=".exe - ";
				var len=s.length;
				var sp=readline.indexOf(".exe - ");
				var start=parseInt(sp)+parseInt(len);
				num=ss.substring(start,start+1);
				failednum=parseInt(num);
				if(failednum==0)
					ret=true;
				break;
			}
			if(!outputfile.AtEndOfStream)
				readline=outputfile.ReadLine();
			else
				break;
		}

		outputfile.Close();
	}
	return ret;
}


function Deploy(buildversion){
	var tDir, today;
	var sDir="bin";
	today=new Date();
	var sdate=today.getFullYear().toString()+(today.getMonth()+1).toString()+(today.getDate()+100).toString().substr(1,2);
	tDir=buildversion+"_"+sdate;

	if(!fso.FolderExists(tDir))
		fso.CreateFolder(tDir);

	fso.CopyFolder(sDir, tDir, true);	
}

function QQSendMail(buildversion, deBugbuildOutputFile, deBugbuildResult){
	var iMsg, iConf;
	iMsg=WScript.CreateObject("CDO.Message");
	iConf=WScript.CreateObject("CDO.Configuration");
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing")=2;
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.qq.com";
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=465;
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")=1;
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername")="602924803@qq.com";
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword")="osgis!!!";
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl")=1;
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout")=60;
	iConf.Fields.Update();

	iMsg.Configuration=iConf;
	iMsg.From="602924803@qq.com";
	iMsg.To="czldl@tom.com";

	iMsg.AddAttachment(path+"\\log\\testlog.txt");
	iMsg.AddAttachment(path+"\\bin\\testioteng.exe");
	iMsg.AddAttachment(path+"\\src\\testioteng.c");
	iMsg.AddAttachment(path+"\\src\\ioteng.c");
	iMsg.AddAttachment(path+"\\src\\ioteng.h");

	var TextBody;
	if(deBugbuildResult==true)
		TextBody=" DailyBuild Successed!";
	else
		TextBody=" DailyBuild Failed!";
	iMsg.Subject=TextBody;
	iMsg.TextBody=deBugbuildOutputFile+TextBody;

	iMsg.Send();//去掉杀毒软件监控和拦截!
	WScript.Echo("QQSendMail: iotenglib!" + TextBody);
}
function TomSendMail(buildversion, deBugbuildOutputFile, deBugbuildResult){
	var iMsg, iConf;
	iMsg=WScript.CreateObject("CDO.Message");
	iConf=WScript.CreateObject("CDO.Configuration");
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing")=2;
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.tom.com";
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25;
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")=1;
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername")="czldl@tom.com";
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword")="osgis123";
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl")=0;
	iConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout")=60;
	iConf.Fields.Update();

	iMsg.Configuration=iConf;
	iMsg.To="602924803@qq.com";
	iMsg.From="czldl@tom.com";

	iMsg.AddAttachment(path+"\\log\\testlog.txt");
	iMsg.AddAttachment(path+"\\bin\\testioteng.exe");
	iMsg.AddAttachment(path+"\\src\\testioteng.c");
	iMsg.AddAttachment(path+"\\src\\ioteng.c");
	iMsg.AddAttachment(path+"\\src\\ioteng.h");
	
	var TextBody;
	if(deBugbuildResult==true)
		TextBody=" DailyBuild Successed!";
	else
		TextBody=" DailyBuild Failed!";
	iMsg.Subject=TextBody;
	iMsg.TextBody=deBugbuildOutputFile+TextBody;

	iMsg.Send();//去掉杀毒软件监控和拦截!
	WScript.Echo("TomSendMail: iotenglib!" + TextBody);
}